# Defines the settings, challenges, log and scores DB values 

import os
from sqlobject import *





settingsDB = os.path.abspath( 'dpc.db' )
scoresDB = os.path.abspath( 'scores.db' )
logDB = os.path.abspath( 'log.db' )



sqlhub.processConnection = connectionForURI( 'sqlite:./dpchallenge.db' )




class settingsTable( SQLObject ):
  # Proxy settings
  proxy_address = StringCol( default = "" )
  proxy_port = StringCol( default = "" )
  proxy_username = StringCol( default = "" )
  proxy_password = StringCol( default = "" )

  # User settings
  login_username = StringCol( default = "" )
  login_password = StringCol( default = "" )

  # Loggin settings
  logging_i = BoolCol( default = True )
  logging_w = BoolCol( default = True )
  logging_e = BoolCol( default = True )

  # Update settings
  update_auto = BoolCol( default = True )
  update_period = IntCol( default = 5 )

  # Chart settings
  charg_genSize = IntCol( default = 400 )
  chart_histResolution = IntCol( default = 4 )

  # Clipboard settings
  chartClip_summary = IntCol( default = True )
  chartClip_scores = IntCol( default = True )
  chartClip_histogram = IntCol( default = True )

  # Notification settings
  notify_popup = BoolCol( default = True )
  notify_popupTime = IntCol( default = 5 )
  notify_sound = BoolCol( default = False )

  notify_ename = BoolCol( default = True )
  notify_cname = BoolCol( default = True )
  notify_lastVote = BoolCol( default = True )
  notify_votes = BoolCol( default = True )
  notify_views = BoolCol( default = True )
  notify_score = BoolCol( default = True )
  notify_comments = BoolCol( default = True )
  notify_favourites = BoolCol( default = True )
  notify_wishList = BoolCol( default = True )


  # Helper functions
  @staticmethod
  def getSettings():
    # Perform actions depending on. Only ever want one 
    # instance in the DB
    count = settingsTable.select().count()
    if count > 1 or count == 0:
      settingsTable.dropTable()
      settingsTable.createTable( ifNotExists = True );
      settingsTable()

    return settingsTable.get( 1 )


  def __getitem__( self, field ):
    if field is "proxy_address":
      return self.proxy_address

    elif field is "proxy_port":
      return self.proxy_port

    elif field is "proxy_username":
      return self.proxy_username

    elif field is "proxy_password":
      return self.proxy_password

    elif field is "login_username":
      return self.login_username

    elif field is "login_password":
      return self.login_password

    elif field is "logging_i":
      return self.logging_i

    elif field is "logging_w":
      return self.logging_w

    elif field is "logging_e":
      return self.logging_e

    elif field is "update_auto":
      return self.update_auto

    elif field is "update_period":
      return self.update_period

    elif field is "charg_genSize":
      return self.charg_genSize

    elif field is "chart_histResolution":
      return self.chart_histResolution

    elif field is "chartClip_summary":
      return self.chartClip_summary

    elif field is "chartClip_scores":
      return self.chartClip_scores

    elif field is "chartClip_histogram":
      return self.chartClip_histogram

    elif field is "notify_popup":
      return self.notify_popup

    elif field is "notify_sound":
      return self.notify_sound

    elif field is "notify_ename":
      return self.notify_ename

    elif field is "notify_cname":
      return self.notify_cname

    elif field is "notify_lastVote":
      return self.notify_lastVote

    elif field is "notify_votes":
      return self.notify_votes

    elif field is "notify_views":
      return self.notify_views

    elif field is "notify_score":
      return self.notify_score

    elif field is "notify_comments":
      return self.notify_comments

    elif field is "notify_favourites":
      return self.notify_favourites

    elif field is "notify_wishList":
      return self.chartClip_histogram

    elif field is "notify_popupTime":
      return self.notify_popupTime

    return None



  def setField( self, field, val ):
    # Change the settings accordingly
    if field is "proxy_address":
      self.proxy_address = val

    elif field is "proxy_port":
      self.proxy_port = val

    elif field is "proxy_username":
      self.proxy_username = val

    elif field is "proxy_password":
      self.proxy_password = val

    elif field is "login_username":
      self.login_username = val

    elif field is "login_password":
      self.login_password = val

    elif field is "logging_i":
      self.logging_i = val

    elif field is "logging_w":
      self.logging_w = val

    elif field is "logging_e":
      self.logging_e = val

    elif field is "update_auto":
      self.update_auto = val

    elif field is "update_period":
      self.update_period = val

    elif field is "charg_genSize":
      self.charg_genSize = val

    elif field is "chart_histResolution":
      self.chart_histResolution = val

    elif field is "chartClip_summary":
      self.chartClip_summary = val

    elif field is "chartClip_scores":
      self.chartClip_scores = val

    elif field is "chartClip_histogram":
      self.chartClip_histogram = val

    elif field is "notify_popup":
      self.notify_popup = val

    elif field is "notify_sound":
      self.notify_sound = val

    elif field is "notify_ename":
      self.notify_ename = val

    elif field is "notify_cname":
      self.notify_cname = val

    elif field is "notify_lastVote":
      self.notify_lastVote = val

    elif field is "notify_votes":
      self.notify_votes = val

    elif field is "notify_views":
      self.notify_views = val

    elif field is "notify_score":
      self.notify_score = val

    elif field is "notify_comments":
      self.notify_comments = val

    elif field is "notify_favourites":
      self.notify_favourites = val

    elif field is "notify_wishList":
      self.notify_wishList = val

    elif field is "notify_popupTime":
      self.notify_popupTime = val

